#ifndef _EVENTLOOP_H
#define _EVENTLOOP_H
#include "mutexLock.h"
#include <my_head.h>
#include <vector>
#include <map>
#include <functional>
#include <memory>
using std::function;
using std::map;
using std::shared_ptr;
using std::vector;
class Acceptor;
class TcpConnection;
using TcpConnectionPtr = shared_ptr<TcpConnection>;
using Callback = function<void(TcpConnectionPtr)>;
using Functor = function<void()>;
class EventLoop
{
public:
    EventLoop(Acceptor &acceptor);
    ~EventLoop();
    void setAllCallbacks(Callback &&, Callback &&, Callback &&);
    void loop();
    void unloop();
    void runinLoop(Functor &&cb);

private:
    int createEpollFd();
    void addEpollReadEvent(int);
    void delEpollReadEvent(int);
    void waitEpollFd();
    void handleNewConnection();
    void handleMessage(int);

    void doPendingFunctors();
    int createEventfd();
    void handleReadEvent();
    void wakeup();

private:
    int _epfd;
    int _eventfd;
    Acceptor &_acceptor;
    bool _isLooping;
    vector<struct epoll_event> _evtArr;
    map<int, TcpConnectionPtr> _conns;
    Callback _onConnection;
    Callback _onMessage;
    Callback _onClose;
    vector<Functor> _pendingFunctors; // 存储等待处理的任务
    MutexLock _mutex;
};

#endif
